﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using eeenno.ReaderEngine.Utilities;

namespace eeenno.SpeeedReaderUI.NativeWebBrowser.Windows.Forms
{
    /// <summary>
    /// Encapsulates the web browser control because we use two different browsers depending on the OS version
    /// </summary>
    public class WebBrowserEx : ControlEx
    {
        private bool usingBrowser65;
        private WebBrowser browser_65 = null;
        private System.Windows.Forms.WebBrowser browser_61 = null;

        public WebBrowserEx() : base(true)
        {
            if (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 5)
            {
                browser_65 = new WebBrowser();
                usingBrowser65 = true;
            }
            else
            {
                browser_61 = new System.Windows.Forms.WebBrowser();
            }
        }

        public void Show()
        {
            if(usingBrowser65)
            {
                browser_65.Show();
            }
            else
            {
                browser_61.Show();
            }
        }

        public void Invoke(Delegate d)
        {
            if(usingBrowser65)
            {
                browser_65.Invoke(d);
            }
            else
            {
                browser_61.Invoke(d);
            }
        }

        public void Invoke(Delegate d, Object[] o)
        {
            if(usingBrowser65)
            {
                browser_65.Invoke(d, o);
            }
            else
            {
                browser_61.Invoke(d, o);
            }
        }

        public void BringFront()
        {
            if (usingBrowser65)
            {
                browser_65.BringToFront();
            }
            else
            {
                browser_61.BringToFront();
            }
        }

        public void SendBack()
        {
            if (usingBrowser65)
            {
                browser_65.SendToBack();
            }
            else
            {
                browser_61.SendToBack();
            }
        }

        public void Clear()
        {
            if (usingBrowser65)
            {
                browser_65.Clear();
            }
        }

        public override DockStyle Dock
        {
            get
            {
                if (usingBrowser65)
                {
                    return browser_65.Dock;
                }
                else
                {
                    return browser_61.Dock;
                }
            }
            set
            {
                if (usingBrowser65)
                {
                    browser_65.Dock = value;
                }
                else
                {
                    browser_61.Dock = value;
                }
            }
        }

        public System.Drawing.Size Size
        {
            get
            {
                if (usingBrowser65)
                {
                    return browser_65.Size;
                }
                else
                {
                    return browser_61.Size;
                }
            }
            set
            {
                if (usingBrowser65)
                {
                    browser_65.Size = value;
                }
                else
                {
                    browser_61.Size = value;
                }
            }
        }

        public System.Drawing.Point Location
        {
            get
            {
                if (usingBrowser65)
                {
                    return browser_65.Location;
                }
                else
                {
                    return browser_61.Location;
                }
            }
            set
            {
                if (usingBrowser65)
                {
                    browser_65.Location= value;
                }
                else
                {
                    browser_61.Location = value;
                }
            }
        }

        public string Url
        {
            get
            {
                if (usingBrowser65)
                {
                    return browser_65.Url;
                }
                else
                {
                    return browser_61.Url.ToString();
                }
            }
            set
            {
                if (usingBrowser65)
                {
                    browser_65.Url = value;
                }
                else
                {
                    if (value != null)
                    {
                        browser_61.Url = new Uri(value);
                    }
                }
            }
        }

        public string DocumentText
        {
            set
            {
                if (usingBrowser65)
                {
                    browser_65.DocumentText = value;
                }
                else
                {
                    browser_61.DocumentText = value;
                }
            }
        }

        public bool Visible
        {
            get
            {
                if(usingBrowser65)
                {
                    return browser_65.Visible;
                }
                else
                {
                    return browser_61.Visible;
                }
            }
            set
            {
                if(usingBrowser65)
                {
                    browser_65.Visible = value;
                }
                else
                {
                    browser_61.Visible = value;
                }
            }
        }

        public override bool InvokeRequired
        {
            get
            {
                if (usingBrowser65)
                {
                    return browser_65.InvokeRequired;
                }
                else
                {
                    return browser_61.InvokeRequired;
                }
            }
        }
    
    }
}
